package com.itheima;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class POIDemo {

	public static void main(String[] args) throws IOException {


		//1.获取工作薄对象【execl】
		XSSFWorkbook workbook = new XSSFWorkbook("d:\\home\\test2.xlsx");
		//2.获取工作表[sheet]
		//2.1根据索引获取工作表
		XSSFSheet sheet = workbook.getSheetAt(0);
		System.out.println(sheet);
		//2.2根据工作表的名称获取
//        XSSFSheet sheet1 = workbook.getSheet("Sheet1");
//        System.out.println(sheet1);
		//3. 获取行对象
//        XSSFRow row = sheet.getRow(1);
//        System.out.println(row);
		//获取最后一行的索引
//        int lastRowNum = sheet.getLastRowNum();
//        System.out.println(lastRowNum);
		//遍历的所有的行
		for (int i = 0; i <= sheet.getLastRowNum(); i++) {
			XSSFRow row = sheet.getRow(i);
//            System.out.println(row);
			//根据索引获取单元格
//            XSSFCell cell = row.getCell(0);
//            System.out.println(cell);
			//获取的该行有多少个单元格
//            short lastCellNum = row.getLastCellNum();
//            System.out.println(lastCellNum);
			for (int j = 0; j < row.getLastCellNum(); j++) {
				XSSFCell cell = row.getCell(j);
//                System.out.println(cell);
				//如果单元格类型是数值类型，设置为字符串类型
				if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
					cell.setCellType(Cell.CELL_TYPE_STRING);
				}
				String cellValue = cell.getStringCellValue();
				System.out.println(cellValue);

				//获取单元格的类型
//                int cellType = cell.getCellType();
//                System.out.println(cellType);
				//改变单元格的类型
//                cell.setCellType(Cell.CELL_TYPE_STRING);
			}
		}

	}
}